import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.WatchedEvent;
import java.util.List;

public class ZkInspector {
    private static ZooKeeper zk;
    
    public static void main(String[] args) throws Exception {
        zk = new ZooKeeper("localhost:2181", 5000, new Watcher() {
            public void process(WatchedEvent event) {
                System.out.println("ZooKeeper event: " + event);
            }
        });
        
        // Wait for connection
        Thread.sleep(3000);
        
        System.out.println("=== ZooKeeper 节点结构检查 ===");
        
        // 检查根节点
        listChildren("/", 0);
        
        // 检查 minfs 节点
        if (exists("/minfs")) {
            listChildren("/minfs", 1);
            
            // 检查 meta 节点
            if (exists("/minfs/meta")) {
                listChildren("/minfs/meta", 2);
                
                // 检查 instances 节点
                if (exists("/minfs/meta/instances")) {
                    System.out.println("  检查 /minfs/meta/instances 中的MetaServer实例:");
                    listChildren("/minfs/meta/instances", 3);
                    
                    // 显示每个实例的详细信息
                    List<String> instances = zk.getChildren("/minfs/meta/instances", false);
                    for (String instance : instances) {
                        String path = "/minfs/meta/instances/" + instance;
                        byte[] data = zk.getData(path, false, null);
                        System.out.println("    实例 " + instance + " 数据: " + new String(data));
                    }
                }
                
                // 检查其他meta子节点
                if (exists("/minfs/meta/leader-election")) {
                    System.out.println("  检查 /minfs/meta/leader-election:");
                    byte[] leaderData = zk.getData("/minfs/meta/leader-election", false, null);
                    System.out.println("    Leader选举数据: " + new String(leaderData));
                }
                
                if (exists("/minfs/meta/leader")) {
                    System.out.println("  检查 /minfs/meta/leader:");
                    byte[] leaderData = zk.getData("/minfs/meta/leader", false, null);
                    System.out.println("    Leader数据: " + new String(leaderData));
                }
            }
            
            // 检查 dataservers 节点
            if (exists("/minfs/dataservers")) {
                listChildren("/minfs/dataservers", 2);
            }
        }
        
        zk.close();
    }
    
    private static boolean exists(String path) {
        try {
            return zk.exists(path, false) != null;
        } catch (Exception e) {
            return false;
        }
    }
    
    private static void listChildren(String path, int indent) {
        try {
            List<String> children = zk.getChildren(path, false);
            String indentStr = "  ".repeat(indent);
            System.out.println(indentStr + path + " 子节点: " + children);
        } catch (Exception e) {
            String indentStr = "  ".repeat(indent);
            System.out.println(indentStr + path + " - 无法访问: " + e.getMessage());
        }
    }
}
